package com.oa.framework.acc.account.domain;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.oa.common.mybatis.typehandler.Boolean2Number;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户账号安全信息
 * @author  cuichao
 */
@Getter
@Setter
public class UserSecurityInfo implements UserDetails {

    /**
     * 启用状态
     */
    public static transient final int ACCOUNT_SECURITY_ENABLE_STATUS = 0;
    /**
     * 锁定
     */
    public static transient final int ACCOUNT_SECURITY_LOCKED_STATUS = 1;
    /**
     * 账户过期
     */
    public static transient final int ACCOUNT_SECURITY_EXPIRED_STATUS = 2;
    /**
     * 用户密码过期
     */
    public static transient final int ACCOUNT_SECURITY_CREDENTIALS_EXPIRED_STATUS = 3;
    /**
     * 禁用状态
     */
    public static transient final int ACCOUNT_SECURITY_DISABLE_STATUS = 4;

    /**
     * userId
     */
    @TableId(value = "account_id", type = IdType.ASSIGN_ID)
    private Long accountId;
    /**
     * 账号名
     */
    @TableField(value = "account_name")
    private String accountName;
    /**
     * 账户编号
     */
    @TableField(value = "account_code")
    private String accountCode;
    /**
     * 用户名
     */
    @TableField(value = "username")
    private String systemUsername;
    /**
     * 密码
     */
    @TableField(value = "password")
    private String password;
    /**
     * 用户状态
     */
    @TableField(value = "security_status")
    private Short securityStatus = null;
    /**
     * 用户的资源权限集合
     */
    @TableField(exist = false)
    private Set<String> permissions;
    /**
     * 权限列表
     */
    @TableField(exist = false)
    private Set<GrantedAuthority> authorities;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(authorities != null){
            return authorities;
        }
        if(CollectionUtils.isEmpty(permissions)){
            return  Collections.emptySet();
        }
        System.err.println(JSON.toJSON(permissions));
        authorities = this.permissions.stream().map(v ->  new SimpleGrantedAuthority(v)).collect(Collectors.toSet());
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return accountName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return securityStatus != ACCOUNT_SECURITY_EXPIRED_STATUS;
    }

    @Override
    public boolean isAccountNonLocked() {
        return securityStatus != ACCOUNT_SECURITY_LOCKED_STATUS;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return securityStatus != ACCOUNT_SECURITY_CREDENTIALS_EXPIRED_STATUS;
    }

    @Override
    public boolean isEnabled() {
        return securityStatus == ACCOUNT_SECURITY_ENABLE_STATUS;
    }

}
